<?php

/**
 * This is the model class for table "{{testimonials}}".
 *
 * The followings are the available columns in table '{{testimonials}}':
 * @property integer $tmID
 * @property integer $userID
 * @property integer $vendorID
 * @property string $testimonial
 * @property string $screened
 * @property string $created
 * @property string $modified
 *
 * The followings are the available model relations:
 * @property Vendor $vendor
 * @property User $user
 */
class Testimonials extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Testimonials the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{testimonials}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('userID, vendorID, testimonial', 'required'),
			array('userID, vendorID', 'numerical', 'integerOnly'=>true),
			array('testimonial', 'length', 'max'=>200),
			array('screened', 'length', 'max'=>20),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('tmID, userID, vendorID, testimonial, screened, created, modified', 'safe', 'on'=>'search'),
				array('modified','default',
           	  'value'=>new CDbExpression('NOW()'),
              'setOnEmpty'=>false,'on'=>'update'),
        	array('created,modified','default',
              'value'=>new CDbExpression('NOW()'),
              'setOnEmpty'=>false,'on'=>'insert'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'vendor' => array(self::BELONGS_TO, 'Vendor', 'vendorID'),
			'user' => array(self::BELONGS_TO, 'User', 'userID'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'tmID' => 'ID',
			'userID' => 'User',
			'vendorID' => 'Brand',
			'testimonial' => 'Testimonial',
			'screened' => 'Show On-screen',
			'created' => 'Posted On',
			'modified' => 'Modified',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('tmID',$this->tmID);
		$criteria->compare('userID',$this->userID);
		$criteria->compare('vendorID',$this->vendorID);
		$criteria->compare('testimonial',$this->testimonial,true);
		$criteria->compare('screened',$this->screened,true);
		$criteria->compare('created',$this->created,true);
		$criteria->compare('modified',$this->modified,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	public static function getList($type,$arrParams)
	{
		switch ($type)
		{
			case 'TESTIMONIAL-LIST':
			return Testimonials::model()->findAll(array('select'=>'*, rand() as rand','limit'=>'10','order'=>'rand'));
			break;
		}
	}
	
}